package com.mhkj.execute.adjust.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.joda.time.DateTime;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.base.templete.service.PlanTempleteService;
import com.mhkj.common.util.ItemHelper;
import com.mhkj.execute.adjust.dao.PlanNodeAdjustRepository;
import com.mhkj.execute.adjust.entity.PlanNodeAdjust;
import com.mhkj.execute.adjust.service.PlanNodeAdjustService;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.util.UserHelper;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 计划节点调整
 * 
 * @author wk
 * @date 2018年8月8日
 * @version 1.0
 */
@Service
public class PlanNodeAdjustServiceImpl extends BaseServiceImpl<PlanNodeAdjust> implements PlanNodeAdjustService {
	@Resource
	private PlanNodeAdjustRepository planNodeAdjustRepository;
	@Resource
	private OrgService orgService;
	@Resource
	private PlanTempleteService planTempleteService;
	@Resource
	private PlanNodeService planNodeService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private ItemHelper itemHelper;

	@Resource
	public void setPlanNodeAdjustRepository(PlanNodeAdjustRepository planNodeAdjustRepository) {
		super.setBaseRepository(planNodeAdjustRepository);
	}

	@Override
	public Pagination<PlanNodeAdjust> listByPage(String yearMonth, PlanNodeAdjust planNodeAdjust, int pageIndex,
			int pageSize) {
		Specification<PlanNodeAdjust> spec = new Specification<PlanNodeAdjust>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<PlanNodeAdjust> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Join<Object, Object> plan = root.join("plan", JoinType.LEFT);
				if (!StringUtils.isEmpty(planNodeAdjust.getNodeName())) {
					lstPredicates.add(criteriaBuilder.like(root.get("nodeName").as(String.class),
							"%" + planNodeAdjust.getNodeName() + "%"));
				}

				if (!StringUtils.isEmpty(yearMonth)) {
					DateTime dateTime = new DateTime(yearMonth);
					// 获取传入月份第一天然后减1就是上月最后一天
					Date mini = dateTime.dayOfMonth().withMinimumValue().minusDays(1).toDate();
					// 结束时间大于上月最后一天
					Date max = dateTime.dayOfMonth().withMaximumValue().plusDays(1).toDate();

					lstPredicates.add(criteriaBuilder.between(root.get("beginDate").as(Date.class), mini, max));
					lstPredicates.add(criteriaBuilder.between(root.get("endDate").as(Date.class), mini, max));
				}

				if (!StringUtils.isEmpty(planNodeAdjust.getAdjustType())) {

					Join<Object, Object> adjustType = root.join("adjustType", JoinType.LEFT);
					Predicate p1 = criteriaBuilder.equal(adjustType.get("id"), planNodeAdjust.getAdjustType().getId());
					lstPredicates.add(p1);
				}

				if (!StringUtils.isEmpty(planNodeAdjust.getPlanNode())) {
					Join<Object, Object> planNode = root.join("planNode", JoinType.LEFT);
					Predicate p2 = criteriaBuilder.equal(planNode.get("id"), planNodeAdjust.getPlanNode().getId());
					lstPredicates.add(p2);
					Predicate p3 = criteriaBuilder.equal(plan.get("id"),
							planNodeAdjust.getPlanNode().getPlan().getId());
					lstPredicates.add(p3);
				}
				Join<Object, Object> item = plan.join("item", JoinType.LEFT);
				if (itemHelper.getCurrentUserItems().size() > 0) {
					Predicate p3 = criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
					Predicate p4 = criteriaBuilder.and(item.isNull());
					lstPredicates.add(criteriaBuilder.or(p3, p4));
				} else {
					Predicate p3 = criteriaBuilder.and(item.isNull());
					lstPredicates.add(p3);
				}
				lstPredicates.add(
						criteriaBuilder.equal(root.get("createBy").as(String.class), UserHelper.getCurrentUserId()));
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<PlanNodeAdjust> pageT = planNodeAdjustRepository.findAll(spec,
				PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
		Pagination<PlanNodeAdjust> pagination = new Pagination<PlanNodeAdjust>();
		pagination.setTotal(planNodeAdjustRepository.count(spec));
		List<PlanNodeAdjust> plans = pageT.getContent();
		for (PlanNodeAdjust planNodeAdjust2 : plans) {
			if (StringUtils.hasText(planNodeAdjust2.getCreateBy())) {
				Employee manager = orgService.employeeInfo(planNodeAdjust2.getCreateBy());
				planNodeAdjust2.setCreateBy(manager.getName());
			}
		}
		pagination.setData(plans);
		return pagination;
	}

}
